package controllers;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.*;

import org.apache.jasper.tagplugins.jstl.core.Redirect;

import models.Area;
import models.Benefit;
import models.Customer;
import models.Location;
import models.SolutionSubmission;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class AddLocationServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");

		Date today = new java.util.Date();
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		String user_email = user.getEmail().toLowerCase();
		
		
		String LocationName = String.valueOf(req.getParameter("LocationName"));
		String locationRegionName = String.valueOf(req.getParameter("locationRegionName"));
		
		
		int RegionID = Integer.parseInt("0");
		String locationCode = String.valueOf(req.getParameter("locationCode"));
	
		
		
		String id = String.valueOf(req.getParameter("id"));
		String Action = String.valueOf(req.getParameter("Action"));
	
		
		Objectify ofy = ObjectifyService.begin();
		int lastIndex = Integer.parseInt("0");
		
		try {
			
			Query<Location> qCount1 = ofy.query(Location.class).filter("LocationRegionName",locationRegionName);
			if (qCount1.countAll() > 0)
			{
				for ( Location f : qCount1.fetch() ) {
					RegionID =f.getLocationRegionID();
					break;
				}
			}
			
			
			if (Action.toUpperCase().trim().equalsIgnoreCase("UPDATE"))
			{
				Location edititem = ofy.get(Location.class,Long.parseLong(id));
				edititem.setLocationName(LocationName);
				edititem.setLocationCode(locationCode);
				edititem.setLocationRegionID(RegionID);
				edititem.setLocationRegionName(locationRegionName);
				edititem.setSubmittedBY(user_email);
				edititem.setSubmittedON(today);
				ofy.put(edititem);

			}
			else
			{
				Query<Location> qCount = ofy.query(Location.class).order("-LocationIndex");
				if (qCount.countAll() > 0)
				{
					for ( Location f : qCount.fetch() ) {
						lastIndex =f.getLocationIndex();
						break;
					}
				}
				Query<Location> q = ofy.query(Location.class).filter("LocationName",LocationName);
				if (q.countAll() <= 0) {
					Location newitems = new Location();
					newitems.setLocationIndex(lastIndex +1);
					newitems.setLocationName(LocationName);
					newitems.setLocationCode(locationCode);
					newitems.setLocationRegionID(RegionID);
					newitems.setLocationRegionName(locationRegionName);
					newitems.setSubmittedBY(user_email);
					newitems.setSubmittedON(today);
					ofy.put(newitems);
				}
			}
			
		
			resp.sendRedirect("/modules/admin/location-admin.jsp");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
} // Class End
